package com.mldong.modules.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.mldong.dict.CustomDictService;
import com.mldong.dict.model.DictItemModel;
import com.mldong.dict.model.DictModel;
import io.swagger.annotations.ApiModel;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class GetAllServiceCustonDictService implements CustomDictService {
    @Override
    public DictModel getByDictKey(Map<String, Object> args) {
        if(ObjectUtil.equals("get_all_service",args.get("dictType"))){
            return getDictModel();
        }
        return null;
    }

    @Override
    public DictModel getDictModel() {
        DictModel dictModel =new DictModel();
        dictModel.setName("所有业务逻辑服务");
        dictModel.setDictKey("get_all_service");
        dictModel.setItems(new ArrayList<>());
        Map<String,IService> map = SpringUtil.getBeansOfType(IService.class);
        map.forEach((key,value)->{
            DictItemModel dictItemModel = new DictItemModel();
            ApiModel apiModel = (ApiModel) value.getEntityClass().getAnnotation(ApiModel.class);
            if(apiModel!=null){
                dictItemModel.setName(apiModel.description());
            } else {
                dictItemModel.setName(key);
            }
            dictItemModel.setDictItemValue(key);
            dictModel.getItems().add(dictItemModel);
        });
        return dictModel;
    }
}
